using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class RenameTable : Transform
    {
        Dictionary<string, string> list;
        
        public RenameTable()
        {
            list = new Dictionary<string,string>();
        }

        public void add(string key, string val)
        {
            list.Add(key, val);
        }

        public RenameTable(Dictionary<string,string> input)
        {
            list = input;
        }

        internal override void Expand(AddColumn ac, Command c)
        {
            if (list.ContainsKey(ac.Table))
                ac.Table = list[ac.Table];
        }

        internal override void Expand(AddElement ae, Command c)
        {
            if (list.ContainsKey(ae.Table))
                ae.Table = list[ae.Table];
        }

        internal override void Expand(AddTable at, Command c)
        {
            if (list.ContainsKey(at.Table))
                at.Table = list[at.Table];
        }

        internal override void Expand(AlterColumnRename acr, Command c)
        {
            if (list.ContainsKey(acr.Table))
                acr.Table = list[acr.Table];
        }

        internal override void Expand(AlterElementRename aer, Command c)
        {
            if (list.ContainsKey(aer.Table))
                aer.Table = list[aer.Table];
        }

        internal override void Expand(AlterTableRename atr, Command c)
        {
            if (list.ContainsKey(atr.OldName))
            {
                if (list.ContainsKey(atr.NewName))
                {
                    // Need to switch the old name to the renamed-old-name, and the new name to the renamed-new-name
                    atr.OldName = list[atr.OldName];
                    atr.NewName = list[atr.NewName];
                }
                else
                {
                    // Need to update the internals to match the new name
                    list[atr.NewName] = list[atr.OldName];
                    list.Remove(atr.OldName);
                }
            }
        }

        internal override void Expand(Delete d, Command c)
        {
            if (list.ContainsKey(d.Table))
                d.Table = list[d.Table];
        }

        internal override void Expand(DropColumn dc, Command c)
        {
            if (list.ContainsKey(dc.Table))
                dc.Table = list[dc.Table];
        }

        internal override void Expand(DropElement de, Command c)
        {
            if (list.ContainsKey(de.Table))
                de.Table = list[de.Table];
        }

        internal override void Expand(DropTable dt, Command c)
        {
            if (list.ContainsKey(dt.Table))
                dt.Table = list[dt.Table];
        }

        internal override void Expand(ForeignKey fk, Command c)
        {
            if (list.ContainsKey(fk.ChildTable))
                fk.ChildTable = list[fk.ChildTable];
            if (list.ContainsKey(fk.ParentTable))
                fk.ParentTable = list[fk.ParentTable];
        }

        internal override void Expand(Insert i, Command c)
        {
            if (list.ContainsKey(i.Table))
                i.Table = list[i.Table];
        }

        internal override void Expand(TableRef tr, Command c)
        {
            if (list.ContainsKey(tr.Table))
                tr.Table = list[tr.Table];
        }

        internal override void Expand(Update u, Command c)
        {
            if (list.ContainsKey(u.Table))
                u.Table = list[u.Table];
        }
    }
}
